package com.general.controller;

import com.general.model.PageBean;
import com.general.model.User;
import com.general.service.UserService;
import com.general.util.MyResult;
import com.general.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiechur on 2016/8/25.
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 查询用户列表
     * @return
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public Object selectUser(){
        List<User> userList = userService.selectAll();
        return MyResult.getResult(1,"",userList);
    }


    /*
   * 分页查询
   * @param page
   * @param rows
   * @return
   * */
    @RequestMapping("/userList")
    @ResponseBody
    public Object userList(@RequestParam(value = "page",required = false)String page,
                           @RequestParam(value = "rows",required = false) String rows,
                           User user)throws Exception{
        PageBean pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("userName", StringUtil.formatLike(user.getUserName()));
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<User> userList=userService.find(map);
        Long total=userService.getTotal(map);
        JSONObject result=new JSONObject();
        JSONArray jsonArray= JSONArray.fromObject(userList);
        result.put("rows", jsonArray);
        result.put("total", total);
        System.out.println(result.toString());
        return result;
    }




}
